<?php
/**
 * @version $Id: approval_manager.php 181 2008-03-11 21:46:28Z mikhail.miguel $
 * @author  Takeshi Kuriyama <kuri@keynext.co.jp>
 */

require_once dirname(dirname(__FILE__)).'/include/gticket.php';

$start = isset($_GET['start'])? intval($_GET['start']) : 0;

// obtain class instances
$db =& Database::getInstance();
$myts =& MyTextSanitizer::getInstance();

$entry_handler =& $myModule->getHandler('entry');
//$com_handler =& $myModule->getHandler('comment');
$tb_handler =& $myModule->getHandler('trackback');

$xoopsGTicket = new xoopsGTicket();
$return_message = array();

if(isset($_POST['approve_tb']) || isset($_POST['delete_tb'])) {
    // check ticket
    if (!$xoopsGTicket->check(true,'d3blog_admin'))
        die($xoopsGTicket->getErrors());

    $bids = array();
    if(isset($_POST['delete_tb']) and is_array($_POST['delete_tb'])) {
        $criteria = new criteriaCompo();
        foreach(array_keys($_POST['delete_tb']) as $bid=>$tid_array) {
            $bids[] = $bid;
            foreach(array_keys($tid_array) as $tid) {
            	$criteria->add(new criteria('tid', intval($tid)), 'OR');
            }
        }
                
        if($tb_handler->deletes($criteria)) {
            $return_message[] = sprintf(_MD_A_D3BLOG_MESSAGE_DB_DELETE_SUCCESS, 'trackback');
    	}
    }

    if(isset($_POST['approve_tb']) and is_array($_POST['approve_tb'])) {
        foreach($_POST['approve_tb'] as $bid=>$tid_array) {
            $bids[] = $bid;
            foreach(array_keys($tid_array) as $tid) {
                $tb = $tb_handler->get($tid);
                $tb->setVar('approved', 1);
				$tb_handler->insert($tb);
            }
        }
		$return_message[] = sprintf(_MD_A_D3BLOG_MESSAGE_DB_UPDATE_SUCCESS, 'trackback');
    }
    
    // SYNCHRONIZE TRACKBACK COUNT
    $bids = array_unique($bids);
    foreach($bids as $bid) {
        $entry = $entry_handler->get($bid);
        $entry->synchronizeTrackbacks();
    }
	$return_message[] = sprintf(_MD_A_D3BLOG_MESSAGE_DB_UPDATE_SUCCESS, 'entry '.$entry->title());

}
/*
if(isset($_POST['approve_com']) || isset($_POST['delete_com'])) {
    // check ticket
    if (!$xoopsGTicket->check(true,'d3blog_admin'))
        die($xoopsGTicket->getErrors());

	$member_handler =& xoops_gethandler('member');
    $bids = array();
    if(isset($_POST['delete_com']) and is_array($_POST['delete_com'])) {
        $criteria = new criteriaCompo();
        foreach(array_keys($_POST['delete_com']) as $bid=>$tid_array) {
            $bids[] = $bid;
        	foreach(array_keys($tid_array) as $comid) {
            	$criteria->add(new criteria('com_id', intval($com_id)), 'OR');
        	}

	        $comments =& $com_handler->getObjects($criteria);
    	    if(count($comments)) {
        	    $deleted_num = array();
            	foreach($comments as $comment) {
            		// store commenter ID and deleted post number into array for later use
                	$commenter_id = $comment->getVar('user_id');
                	if($commenter_id != 0 && $comment->getVar('com_status') == D3BLOG_COM_ACTIVE) {
                    	$deleted_num[$commenter_id] = !isset($deleted_num[$commenter_id]) ? 1 : ($deleted_num[$commenter_id] + 1);
                	}
            	}
            	// discrement commenters post
            	foreach($deleted_num as $user_id => $post_num) {
                	// update user posts
                	$commenter = $member_handler->getUser($user_id);
                	if(is_object($commenter)) {
                    	$member_handler->updateUserByField($commenter, 'posts', $commenter->getVar('posts') - $post_num);
                	}
            	}
        	}

			// and finally delete comments
			foreach(array_keys($_POST['delete_com']) as $comid) {
				$comment = $com_handler->get(intval($comid));
				$com_handler->deleteByThread($comment);
			}
    	    $return_message[] = sprintf(_MD_A_D3BLOG_MESSAGE_DB_DELETE_SUCCESS, 'comment');
        }
    }

    if(isset($_POST['approve_com']) and is_array($_POST['approve_com'])) {
        foreach($_POST['approve_com'] as $bid=>$comid_array) {
            $bids[] = $bid;
            foreach(array_keys($comid_array) as $comid) {
                $comment = $com_handler->get($comid);
                $former_status = $comment->getVar('com_status');
                $comment->setVar('com_status', D3BLOG_COM_ACTIVE);
                if($com_handler->insert($comment)) {
                    $increment = $comment->statusChanged();
                    if($increment != 0) {
                    	$bids[] = $comment->getVar('com_bid');
                    	//INCREMENT COMMENTER'S POSTS
                    	$comment->incrementUserPost($increment);
                    }

                }
            }
        }
        $return_message[] = sprintf(_MD_A_D3BLOG_MESSAGE_DB_UPDATE_SUCCESS, 'comment');
    }
    
    // SYNCHRONIZE COMMENTS COUNT
    $bids = array_unique($bids);
    foreach($bids as $bid) {
        $entry = $entry_handler->get($bid);
        $entry->synchronizeComments();
    }
	$return_message[] = sprintf(_MD_A_D3BLOG_MESSAGE_DB_UPDATE_SUCCESS, 'entry '.$entry->title());

}*/

if(count($return_message)) {
    $message = "";
    foreach($return_message as $msg) {
        $message .= htmlspecialchars($msg, ENT_QUOTES).'<br />';
    }
    redirect_header(XOOPS_URL."/modules/$mydirname/admin/index.php?page=approval_manager", 1, $message);
    exit;
}
/*
$sql_count = sprintf("SELECT count(e.bid) FROM %s e LEFT JOIN %s t ON t.bid=e.bid LEFTJOIN %s c ON c.com_bid=e.bid",
    $db->prefix($mydirname.'_entry'), $db->prefix($mydirname.'_trackback'), $db->prefix($mydirname.'_comment'));
$sql = sprintf("SELECT e.bid, e.title, e.created as posted, t.tid, t.blog_name, t.excerpt, t.url, t.created as pingbacked, c.com_id, c.user_name, c.com_text, c.user_url, c.created as commented FROM %s e LEFT JOIN %s t ON t.bid=e.bid LEFTJOIN %s c ON c.com_tid=e.bid",
    $db->prefix($mydirname.'_entry'), $db->prefix($mydirname.'_trackback'), $db->prefix($mydirname.'_comment'));
$sql_where = " WHERE e.approved=0 OR (t.direction=2 AND t.approved=0) OR (c.com_type=1 AND c.com_status=1)";
$order = " ORDER BY bid DESC, tid DESC, com_id DESC";*/

$sql_count = sprintf("SELECT count(e.bid) FROM %s e LEFT JOIN %s t ON t.bid=e.bid",
    $db->prefix($mydirname."_entry"), $db->prefix($mydirname."_trackback"));
$sql = sprintf("SELECT e.bid, e.title, e.created as posted, t.tid, t.blog_name, t.excerpt, t.created as transmitted, t.url FROM %s e LEFT JOIN %s t ON t.bid=e.bid",
    $db->prefix($mydirname."_entry"), $db->prefix($mydirname."_trackback"));
$sql_where = " WHERE e.approved = 0 OR (t.direction = 2 AND t.approved = 0)";
$order = " ORDER BY bid, tid";

$res = $db->query($sql_count.$sql_where);
if($res) {
    list($count) = $db->fetchRow($res);
} else {
    $count = 0;
}

$page_navigater = "";
$show_approval = false;
$rows = array();
if($count > 0) {
    $show_approval = true;
    $perpage = $myModule->getConfig('archive_numperpage')? intval($myModule->getConfig('archive_numperpage')) : 30;

    // HOW MANY PAGES
    $page_navigater = '';
    if ( $count > $perpage ) {
        require_once XOOPS_ROOT_PATH.'/class/pagenav.php';
        $nav = new XoopsPageNav($count, $perpage, $start, "start", "");
        $page_navigater = $nav->renderNav();
    }

    // GET DATA TO BE APPROVED
    $result = $db->query($sql.$sql_where.$order, $perpage, $start);
/*
    while(list($bid, $title, $posted, $tid, $blog_name, $excerpt, $url, $pingbacked, $com_id, $user_name, $com_text, $user_url, $commented) = $db->fetchRow($result)) {
        $row['bid'] = intval($bid);
        $row['title'] = $myts->htmlSpecialChars($title);
        $row['posted'] = intval($posted);
        $row['tid'] = intval($tid);
        $row['blog_name'] = $myts->htmlSpecialChars($blog_name);
        $row['excerpt'] = strip_tags($myts->displayTarea(strip_tags($excerpt), 0, 0, 0, 0, 0));
        $row['url'] = $myts->htmlSpecialChars($url);
        $row['pingbacked'] = intval($pingbacked);
        $row['com_id'] = intval($com_id);
        $row['user_name'] = $myts->htmlSpecialChars($user_name);
        $row['com_text'] = strip_tags($myts->displayTarea(strip_tags($com_text), 0, 0, 0, 0, 0));
        $row['user_url'] = $myts->htmlSpecialChars($user_url);
        $row['commented'] = intval($commented);
        $rows[] = $row;
    }*/

    while(list($bid, $title, $posted, $tid, $blog_name, $excerpt, $transmitted) = $db->fetchRow($result)) {
        $row['bid'] = intval($bid);
        $row['title'] = htmlspecialchars($title, ENT_QUOTES);
        $row['posted'] = intval($posted);
        $row['tid'] = intval($tid);
        $row['blog_name'] = htmlspecialchars($blog_name, ENT_QUOTES);
        $row['excerpt'] = strip_tags($myts->displayTarea(strip_tags($excerpt), 0, 0, 0, 0, 0));
//        $row['url'] = htmlspecialchars($url, ENT_QUOTES);
        $row['transmitted'] = intval($transmitted);
        $rows[] = $row;
    }
}

xoops_cp_header();
include dirname(__FILE__).'/mymenu.php';
$tpl = new XoopsTpl();
$tpl->assign( array(
    'myname' => $myModule->module_name,
    'mydirname' => $mydirname4show,
    'mod_url' => sprintf("%s/modules/%s", XOOPS_URL, $mydirname4show),
    'moduleConfig' => $myModule->module_config,
    'page_subtitle' => htmlspecialchars(_MD_A_D3BLOG_LANG_APPROVAL_MANAGER, ENT_QUOTES),
    'show_approval' => $show_approval,
    'counter' => $count,
    'rows' => $rows,
    'page_navigater' => $page_navigater,
    'gticket_hidden' => $xoopsGTicket->getTicketHtml( __LINE__ , 1800 , 'd3blog_admin')
    )
);

//$xoopsTpl->xoops_setDebugging(true);  // smarty debug
$tpl->display( 'db:'.$mydirname.'_admin_approval_manager.html' ) ;
xoops_cp_footer();


?>